﻿using CommunityToolkit.Mvvm.DependencyInjection;
using CommunityToolkit.Mvvm.Input;
using FanaticRock.Core.Models;
using FanaticRock.Core.ServiceInterfaces;
using FanaticRock.Helpers;
using FanaticRock.MediaSource.BilibiliAPI.ResponseDto.Fav.Resource;

namespace FanaticRock.ViewModels
{
    public partial class BvidSongViewModel : SongViewModel
    {
        #region services

        protected IDownloadManageService _downloadManageService;
        protected ILocalStorageService _localStorageService;

        #endregion

        #region fields

        protected string _bvid;

        //private string _avid;

        #endregion

        #region ctors

        public BvidSongViewModel() : base()
        {
            var ioc = Ioc.Default;
            InitIoc(ioc);
        }

        public BvidSongViewModel(VideoInfoDto dto) : this()
        {
            Load(dto);
            var ioc = Ioc.Default;
            InitIoc(ioc);
        }

        public BvidSongViewModel(BvidSongViewModel other) : base(other)
        {
            _bvid = other._bvid;
            var ioc = Ioc.Default;
            InitIoc(ioc);
        }

        #endregion

        #region methods

        private void InitIoc(Ioc ioc)
        {
            _downloadManageService = ioc.GetService<IDownloadManageService>();
            _localStorageService = ioc.GetService<ILocalStorageService>();
        }

        public void Load(VideoInfoDto model)
        {
            _id = model.Id.ToString();
            _title = StringHelper.Normalize(model.Title);
            _originTitle = model.Title;
            _bvid = model.Bvid;
            //_albumId = model.AlbumId;
            //_albumTitle = model.AlbumTitle;
            _duration = TimeSpan.FromSeconds(model.Duration);
            //_uri = model.;
            var miniCover = GetMiniCover(model.Cover);

            _coverSource = new UriResourceSource(miniCover);

            //_members = ObservableCollectionHelper.CreateFromModel<SongMember, SongMemberViewModel>(model.Members);
        }
        private string GetMiniCover(string cover)
        {
            if (string.IsNullOrWhiteSpace(cover)) return string.Empty;
            return $"{cover}@140w_80h_1c.png";
        }

        ///<summary>下载。</summary>
        [RelayCommand]
        public virtual async void Download()
        {
            await DownloadCore();
        }

        internal async Task DownloadCore()
        {
            var taskName = _title;
            var bvid = _bvid;

            string baseUrl = await BvidHelper.GetAudioBaseUrl(bvid);

            var filePath = _localStorageService.GetPath(taskName);

            await _downloadManageService.AddDownloadItem(taskName, baseUrl, filePath);

            var downloadBvidSongViewModel = new Download.DownloadBvidSongViewModel(this);
            downloadBvidSongViewModel.Init(taskName, baseUrl, filePath);

            var mainViewModel = Ioc.Default.GetService<MainViewModel>();
            mainViewModel.AddDownloadItem(downloadBvidSongViewModel);

            NoticeHelper.Notice("歌曲下载完成", $"{_title} 歌曲下载完成");
        }

        #endregion


    }
}
